import base from '../../scripts/base'
import utils from '../../scripts/utils'
import './hbdx.css'
import $ from 'jquery'

$(function(){
    $('#download').on('click', function(){
        window.location.href='download_new.html';
    })
    $('#receive').on('click', function(){
        var str = window.location.hash.substring(1);
        receivegift(str);
    })

    //判断是否是微信
    if(isWeChat()){
        $('body').css('background', 'rgb(0,57,98)');
        $(".div1").show();
        $(".wrap").hide(); 
    }else{
        $(".div1").hide();
        $(".wrap").show();
    }
})


var g_baseScheme = "wxa40027d0ea49a2af://"
//iframe生成器
var createIframe=(function(){
    var iframe;
    return function(){
        if(iframe){
            return iframe;
        }else{
            iframe = document.createElement('iframe');
            iframe.style.display = 'none';
            document.body.appendChild(iframe);
            return iframe;
        }
    }
})()

var isIos = function(){
    var ua = navigator.userAgent.toLowerCase();
    if(/(iPhone|iPad|iPod|iOS)/i.test(ua)){
        return true;
    }
    return false;
}

var isAndroid = function(){
    var ua = navigator.userAgent.toLowerCase();
    if(/Android/i.test(ua)){
        return true;
    }
    return false;
}

var isWeChat = function(){
    var ua = navigator.userAgent.toLowerCase();
    if(/MicroMessenger/i.test(ua)){
        return true;
    }else{
        return false;
    }
}

/*
 打开app
 @param command 和app约定好的命令
 */
var openApp = function(command){
    //启动并进入房间
    var openIframe = createIframe();
    if(isIos()){
        var ver = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/);
        ver = parseInt(ver[1], 10);
        if(ver<9) {
            window.location = command;
            var loadDateTime = Date.now();
            setTimeout(function () {
                var timeOutDateTime = Date.now();
                if (timeOutDateTime - loadDateTime < 1000) {
                    alert('安装注册app即可领取哦~');
                }
            }, 500);
        }else{
            window.location = command;
            var loadDateTime = Date.now();
            setTimeout(function () {
                var timeOutDateTime = Date.now();
                if (timeOutDateTime - loadDateTime < 1000) {
                     alert('安装注册app即可领取哦~');
                }
            }, 500);
        }
    }else{
        //其它平台
        openIframe.src = command;
        var loadDateTime = Date.now();
        setTimeout(function () {
            var timeOutDateTime = Date.now();
            if (timeOutDateTime - loadDateTime < 1000) {
                alert('安装注册app即可领取哦~');
            }
        }, 500);
    }
}

var receivegift = function(opt){
    //获取当前URL
    var thisURL = document.URL;
    var startkey = thisURL.indexOf('?');
    //获取参数
    var param = thisURL.substr(startkey + 1,thisURL.length-1);
    var isappinstalled = true;
    var command = g_baseScheme + "action=bind_channel&token=hbdx"+opt;

    var downloadUrl = "http://hbjxqp.happypoker.cn/download.html";//外网download页面

    if( isappinstalled ){ //如果本机装有APP 执行打开APP流程
        // include_js("./openapp/"+ getQueryString('action') +".js",0);
        if(isWeChat()){
            $('body').css('background', 'rgb(0,57,98)');
            $(".div1").show();
            $(".wrap").hide();
        }else{
            if( param == null ){
                return;
            }
            openApp(command);
        }
    }else{//如果本机没有安装APP 直接跳转下载页面
        if (!isWeChat()){
            window.location.href=downloadUrl;
        }
    }
}

function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}
document.write = function (s)
{
    document.getElementById('jsdiv').innerHTML+=s;
    return false;
}
function include_js(path,reload)
{
    var scripts = document.getElementsByTagName("script");
    if (!reload)
        for (var i=0;i<scripts.length;i++)
            if (scripts[i].src && scripts[i].src.toLowerCase() == path.toLowerCase() ) return;
    var sobj = document.createElement('script');
    sobj.type = "text/javascript";
    sobj.src = path;
    var headobj = document.getElementsByTagName('head')[0];
    headobj.appendChild(sobj);
}